package coupon

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"context"
)

// SetCouponItemsParam 设置优惠券商品配置
type SetCouponItemsParam struct {
	CouponPtr    *sqlc.Coupon
	ItemConfPtrs param.ItemConfigurationPtrArray
}

// SetCouponItems 设置优惠券商品配置
func (s *CouponEntityServiceImp) SetCouponItems(ctx context.Context, q *sqlc.Queries, ptr *SetCouponItemsParam) (err error) {
	couponID := ptr.CouponPtr.ID
	err = q.DisableCouponItemsByCouponID(ctx, couponID)
	if err != nil {
		return
	}
	if len(ptr.ItemConfPtrs) > 0 {
		for idx, itemConf := range ptr.ItemConfPtrs {
			err = q.UpsertCouponItem(ctx, sqlc.UpsertCouponItemParams{
				CouponID: couponID,
				ItemID:   itemConf.ID,
				ItemName: itemConf.Name,
				SortNo:   int32(idx + 1),
			})
			if err != nil {
				return
			}
		}
	}
	return
}
